package ModelLayer;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;

/**
 * Write a description of class AddressBook here.
 * 
 * @author (Jan Skrubbeltrang) 
 * @version (a version number or a date)
 */
public class AddressBook
{
    // instance variables
    private static AddressBook instance;
    private static int index;
    private HashMap<Integer,Person> addressBook;

    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        // initialise instance variables
        addressBook = new HashMap<Integer,Person>();
        index = 0;
    }
    
    /**
     * Accessor for AddressBook instance
     * @return instance: AddressBook
     */
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    
    /**
     * Method to adding an element to the HashMap addressBook
     * @param p: Person
     * @return Person - person object inserted on list
     */
    public Person addPerson(Person p)
    {
        index++;
        p.setId(index);
        addressBook.put(p.getId(),p);
        return p;
    }
    
    /**
     * Accessor for addressBook
     * @param id: int
     * @return id  - index from addressBook
     */
    public Person getPerson(int id)
    {
        return addressBook.get(id);
    }
    
    /**
     * Mutator for person
     * @param p: Person
     */    
    public void setPerson(Person p)
    {
        addressBook.put(p.getId(),p);
    }
    
    /**
     * Method to removing a person object from our HashMap (addressBook)
     * @param id: int
     */
    public void removePerson(int id)
    {
        addressBook.remove(id);
    }
    
    /**
     * Method to getting returned a full list of all Person objects in the HashMap (addressBook)
     * @return persons: ArrayList
     */
    public ArrayList<Person> listAllPersons()
    {
        ArrayList<Person> persons = new ArrayList<Person>();
        Set<Integer> indexes = addressBook.keySet(); 
        for(Integer index : indexes)
        {
            Person p = addressBook.get(index);
            persons.add(p);
        }
        return persons;
    }
}

